package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.CityHeaderVH;
import com.centaline.androidsalesblog.adapter.viewholder.CityVH;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.model.databaseobject.CityDo;
import com.centanet.framework.base.BaseAdapter1;

import java.util.ArrayList;

import ca.barrenechea.widget.recyclerview.decoration.StickyHeaderAdapter;

/**
 * 城市列表
 * Created by guilin on 16/1/6.
 */
public class CityListAdapter extends BaseAdapter1<CityVH> implements
        StickyHeaderAdapter<CityHeaderVH> {

    private final ArrayList<CityDo> cityDoList;
    private final boolean forceSelect;
    private RecyclerViewItemOnClickListener<CityDo> recyclerViewItemOnClickListener;
    private int locationStatus;

    public CityListAdapter(Context context, ArrayList<CityDo> cityDoList, boolean forceSelect) {
        super(context);
        this.cityDoList = cityDoList;
        this.forceSelect = forceSelect;
    }

    public void setRecyclerViewItemOnClickListener(RecyclerViewItemOnClickListener<CityDo> recyclerViewItemOnClickListener) {
        this.recyclerViewItemOnClickListener = recyclerViewItemOnClickListener;
    }

    /**
     * 设置定位状态
     *
     * @param locationStatus 0:定位中;1:定位成功;2:定位失败
     */
    public void setLocationStatus(int locationStatus) {
        this.locationStatus = locationStatus;
        notifyItemChanged(0);
    }

    public int getLocationStatus() {
        return locationStatus;
    }

    @Override
    protected int inflateByLayoutId() {
        return R.layout.item_city;
    }

    @Override
    protected CityVH viewHolder(View view) {
        return new CityVH(view);
    }

    @Override
    public void onBindViewHolder(final CityVH holder, int position) {
        final CityDo cityDo = cityDoList.get(position);
        if (position == 0) {
            holder.atv_name.setText(cityDo.getCityName());
            holder.fl_status.setVisibility(View.VISIBLE);
            holder.progressBar.setVisibility(locationStatus == 0 ? View.VISIBLE : View.GONE);
        } else {
            holder.atv_name.setText(cityDo.getCityName());
            holder.fl_status.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (recyclerViewItemOnClickListener != null)
                    recyclerViewItemOnClickListener.onItemOnClick(cityDo, holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return cityDoList.size();
    }

    @Override
    public long getHeaderId(int position) {
        return position == 0 ? 0 : 1;
    }

    @Override
    public CityHeaderVH onCreateHeaderViewHolder(ViewGroup parent) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_city_header, parent, false);
        return new CityHeaderVH(view);
    }

    @Override
    public void onBindHeaderViewHolder(CityHeaderVH holder, int position) {
        holder.atv_name.setText(position == 0 ? "当前定位城市" : forceSelect ? "您可以直接从下面选择所在的城市" : "全部城市");
    }
}
